<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <script src="${pageContext.request.contextPath}/js/jquery-1.8.3.js"></script>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/layui/css/layui.css">
    <script src="${pageContext.request.contextPath}/layui/layui.js"></script>
</head>
<body>

<!-- 添加的form表单 star-->
<form class="layui-form" id="addForm" style="display: none;padding: 10px 10px 0 0" method="post">
    <div class="layui-form-item">
        <label class="layui-form-label">上师名</label>
        <div class="layui-input-inline">
            <input type="text" name="guruName" placeholder="请输入上师名" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">法号</label>
        <div class="layui-input-inline">
            <input type="text" name="guruNickname" placeholder="请输入法号" autocomplete="off" class="layui-input">
        </div>
    </div>

    <!--《《《《《《《《《上传图片《《《《《《《《《》-->
    <div class="layui-form-item">
        <label class="layui-form-label">图片</label>
        <div class="layui-input-block">
            <input type="hidden" name="guruImage" id="pic1">
            <button type="button" class="layui-btn" id="file1">
                <i class="layui-icon">&#xe67c;</i>点击上传
            </button>
            <img src="" id="guruImage" width="100px" height="60px" style="display: none">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">状态</label>
        <div class="layui-input-block">
            <input type="radio" name="guruStatus" value="0" title="正常" checked>
            <input type="radio" name="guruStatus" value="1" title="冻结">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="*">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<!--添加表单end-->

<div>
    <button type="button" class="layui-btn" id="file2">
        <i class="layui-icon">&#xe67c;</i>批量上传
    </button>
    <button class="layui-btn" id="download">批量下载</button>
</div>

<table id="demo" lay-filter="myTableFilter"></table>

<div>
    <button class="layui-btn" id="addBtn">添加</button>
</div>
<script type="text/html" id="toolbar1">
    <a class="layui-btn layui-btn-xs" lay-event="test1">修改状态</a>
    <a class="layui-btn layui-btn-xs" lay-event="test2">删除</a>
</script>

    <script>
        layui.use(['table','form','upload'], function() {

            var table = layui.table;
            var form = layui.form;
            var upload = layui.upload;

            //渲染 table表格
            table.render({
                elem: '#demo'
                , url: '${pageContext.request.contextPath}/guru/selectPage' //数据接口
                , page: true //开启分页
                , limit: 5
                , limits: [3, 5, 10]
                , cols: [[ //表头
                      {field: 'guruId', title: '编号'}
                    , {field: 'guruName', title: '法师名'}
                    , {field: 'guruStatus', title: '状态'}
                    , {field: 'guruNickname', title: '法号'}
                    , {title: '头像', templet: function (d) {
                            return "<img src='"+"${pageContext.request.contextPath}/"+d.guruImage+"' width=\"100px\" height=\"60px\">";
                        }
                    }
                    , {title: '操作', toolbar: '#toolbar1'}
                ]]
            });

            //添加弹出框start
            $("#addBtn").click(function () {
                layer.open({
                    type:1,
                    content:$("#addForm"),
                    area:['500px','400px'],
                    title:'添加上师'
                })
            })
            //添加弹出框 end

            //logo图片上传 start
            upload.render({
                elem: '#file1'
                ,url: '${pageContext.request.contextPath}/guru/upload'
                ,done: function(res){ //上传后的回调
                    $("#pic1").val(res.pic);
                    $("#guruImage").prop("src","${pageContext.request.contextPath}/"+res.pic);
                    $("#guruImage").prop("style","");
                    layer.msg("图片上传成功");
                }
            })
            //图片上传 end

            //添加 start
            form.on("submit(*)",function() {
                var s = $("#addForm").serialize();
                $.ajax({
                    url:"${pageContext.request.contextPath}/guru/add",
                    data:s,
                    dataType:"json",
                    success:function(result){
                        if(result.flag){
                            layer.closeAll();
                            layer.msg("添加成功");
                            $("#addForm")[0].reset();
                            table.reload('demo',{
                                //从第几页开始
                                page:{curr:1}
                            })
                        }
                    }
                })
                return false;
            })
            //添加 end

            //删除修改start
            table.on('tool(myTableFilter)',function (obj) {
                 if(obj.event=='test1'){
                     //询问弹出框
                     layer.confirm('确定修改吗?',function(index){
                         $.ajax({
                             url:"${pageContext.request.contextPath}/guru/update",
                             data:'guruId='+obj.data.guruId+'&guruStatus='+obj.data.guruStatus,
                             dataType:"json",
                             success:function(result){
                                 if(result.flag){
                                     layer.msg("修改成功");
                                     table.reload('demo',{
                                         //从第几页开始
                                         page:{curr:1}
                                     })
                                 }
                             }
                         })
                     })

                 }else
                // 删除
                if(obj.event=='test2'){
                    //询问弹出框
                    layer.confirm('确定删除吗?',function(index){
                        $.ajax({
                            url:'${pageContext.request.contextPath}/guru/delete',
                            data:'guruId='+obj.data.guruId,
                            dataType:'json',
                            success:function(result){
                                if(result.flag){
                                    layer.msg('删除成功');
                                    table.reload('demo',{
                                        //从第几页开始
                                        page:{curr:1}
                                    })
                                }
                            }
                        })
                    })
                }
            })
            //删除end

            //批量添加上师 start
            upload.render({
                elem: '#file2'
                ,url: '${pageContext.request.contextPath}/guru/uploadsGuru'
                ,accept: 'file' //允许上传的文件类型
                ,exts:'xls'
                //,size: 50 //最大允许上传的文件大小
                ,done: function(res){ //上传后的回调
                    layer.msg("数据上传成功");
                    table.reload('demo',{
                        //从第几页开始
                        page:{curr:1}
                    })
                }
            })
            //批量添加上师 end

            //批量下载 start
            $("#download").click(function () {
                location.href="${pageContext.request.contextPath}/guru/download";
            })

        })
</script>
</body>
</html>
